﻿using BLL;
using BLL.考试完成后显示成绩BLL;
using Model;
using System;
using System.Windows.Forms;
using System.Data;

namespace NCRE学生考试端V1._0.教师端操作类
{
    public partial class FrmStudentSetting : Form
    {
        private string SelectedMode;
        public FrmStudentSetting()
        {
            InitializeComponent();
        }

        #region 窗体单一显示-李金颖-2018-12-8 11:59:14
        private static FrmStudentSetting frmStudentSetting = null;
        public static FrmStudentSetting GetInstance()
        {
            if (frmStudentSetting==null|| frmStudentSetting.IsDisposed)
            {
               frmStudentSetting = new FrmStudentSetting();
            }
            return frmStudentSetting;
        }
        #endregion

        #region 窗体加载事件-李金颖-2018-12-29 16:35:07-增加查询setUp表获取考试时间-谷海涛
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmStudentSetting_Load(object sender, EventArgs e)
        {
            btnCustom.Visible = false;//自定义设置按钮不可见
            gbxSetting.Enabled = false;//保存按钮不可用
            ShowSetting();//加载学生端模式设置                         
            DataTable dt = new LogBll().QuerySetUpBll(); //查询setUp表获取考试时间
            txtExamTime.Text = dt.Rows[0]["examTime"].ToString();
        }
        #endregion

        #region 考试设置保存-李金颖-2018年12月29日-增加考试时间设置-谷海涛-2019年1月2日20:27:34
        /// <summary>
        /// 考试设置保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveSetting_Click(object sender, EventArgs e)
        {
            //查询setUp表获取相关数据
            DataTable dt = new LogBll().QuerySetUpBll();
            bool Ntime = numericUpDown2.Value.ToString() == dt.Rows[0]["noticeTime"].ToString();
            bool Skey = txtScreenKey.Text == dt.Rows[0]["screenlockkey"].ToString();
            bool examTime = txtExamTime.Text == dt.Rows[0]["examTime"].ToString();
            if (Ntime && Skey && examTime)
            {
                MessageBox.Show("没有修改任何项", "考试设置");
                return;
            }

            SetUp enSetUp = new SetUp();
            enSetUp.noticeTime = 1;
            if (!Ntime)
            {
                //更新倒计时
                enSetUp.noticeTime = int.Parse(numericUpDown2.Value.ToString());
                UpdateSetUp(enSetUp, "noticeTime");
            }
            if (!Skey)
            {
                if (IsNumber(txtScreenKey.Text))
                {
                    enSetUp.screenlockkey = int.Parse(txtScreenKey.Text);
                    UpdateSetUp(enSetUp, "screenlockkey");
                }
                else
                {
                    MessageBox.Show("请输入有效数字", "考试设置");
                }
            }
            if (!examTime)//更新考试时间
            {
                if (IsNumber(txtExamTime.Text))
                {
                    enSetUp.examTime = int.Parse(txtExamTime.Text);
                    UpdateSetUp(enSetUp, "examTime");
                }
                else
                {
                    MessageBox.Show("请输入有效数字", "考试设置");
                }
            }
            MessageBox.Show("保存配置成功！");
        }
        #endregion

        #region 自定义设置与保存-李金颖-2018年12月29日
        /// <summary>
        /// 自定义模式设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCustom_Click(object sender, EventArgs e)
        {
            if (btnCustom.Text == "自定义设置")
            {
                btnCustom.Text = "保存设置";
                gbxSetting.Enabled = true;
            }
            else
            {
                btnCustom.Text = "自定义设置";
                SetUp setup = new SetUp();

                #region 成绩显示
                if (checkBox1.Checked)
                {
                    setup.is_showScore = 1;
                }
                else
                {
                    setup.is_showScore = 0;
                }
                UpdateSetUp(setup, "is_showScore");
                #endregion

                #region 上传文件
                if (checkBox2.Checked)
                {
                    setup.is_upload = 0;//上传
                }
                else
                {
                    setup.is_upload = 1;
                }
                UpdateSetUp(setup, "is_upload");
                #endregion

                #region 锁屏
                if (checkBox2.Checked)
                {
                    setup.is_lockScreen = 0;//锁屏
                }
                else
                {
                    setup.is_lockScreen = 1;
                }
                UpdateSetUp(setup, "is_lockScreen");
                #endregion
                gbxSetting.Enabled = false;
            }
        }
        #endregion

        #region 单选框变化触发事件-李金颖-2018年12月29日
        /// <summary>
        /// 单选框变化触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckedChanged(object sender, EventArgs e)
        {
            btnCustom.Visible = false;
            SetUp setup = new SetUp();

            #region 考试模式
            if (radioButton1.Checked)
            {
                setup.is_showScore = 0;//不显示成绩
                UpdateSetUp(setup, "is_showScore");
                setup.is_upload = 0;//上传
                UpdateSetUp(setup, "is_upload");
                setup.is_lockScreen = 0;//锁屏
                UpdateSetUp(setup, "is_lockScreen");
                setup.is_exam = 0;//考试模式
                UpdateSetUp(setup, "is_exam");//李金颖
                gbxSetting.Text = "考试模式";
                checkBox1.Checked = false;
                checkBox2.Checked = true;
                checkBox3.Checked = true;
            }
            #endregion

            #region 练习模式
            if (radioButton2.Checked)
            {
                setup.is_showScore = 0;//不显示成绩
                UpdateSetUp(setup, "is_showScore");
                setup.is_upload = 1;//不上传
                UpdateSetUp(setup, "is_upload");
                setup.is_lockScreen = 1;//不锁屏
                UpdateSetUp(setup, "is_lockScreen");
                setup.is_exam = 1;//练习模式
                UpdateSetUp(setup, "is_exam");//李金颖
                gbxSetting.Text = "练习模式";
                checkBox1.Checked = false;
                checkBox2.Checked = false;
                checkBox3.Checked = false;
            }
            #endregion

            #region 自定义模式
            if (radioButton3.Checked)
            {
                btnCustom.Visible = true;
                setup.is_exam = 2;//自定义模式
                UpdateSetUp(setup, "is_exam");//李金颖
                gbxSetting.Text = "自定义";
                DataTable dt = new LogBll().QuerySetUpBll();
                if (dt.Rows[0]["is_showScore"].ToString() == "1")//显示成绩
                {
                    checkBox1.Checked = true;
                }
                else
                {
                    checkBox1.Checked = false;
                }
                if (dt.Rows[0]["is_upload"].ToString() == "0")//上传
                {
                    checkBox2.Checked = true;
                }
                else
                {
                    checkBox2.Checked = false;
                }
                if (dt.Rows[0]["is_lockScreen"].ToString() == "0")//锁屏
                {
                    checkBox3.Checked = true;
                }
                else
                {
                    checkBox3.Checked = false;
                }

                #endregion

            }
        } 
        #endregion

        #region 工具代码段-姜晶-2018年4月30日

        #region 判断是否为数字-李金颖-2018年12月29日
        /// <summary>
        /// 判断是否为数字
        /// </summary>
        /// <param name="num">要判断的数字</param>
        /// <returns></returns>
        public bool IsNumber(string num)
        {
            int number;
            return int.TryParse(num, out number);
        } 
        #endregion

        #region 获取模式设置与考试设置-李金颖-2018年12月29日
        /// <summary>
        /// 获取模式设置与考试设置
        /// </summary>
        public void ShowSetting()
        {
            DataTable dt = new LogBll().QuerySetUpBll();

            #region 考试设置
            numericUpDown2.Value = decimal.Parse(dt.Rows[0]["noticeTime"].ToString());
            txtScreenKey.Text = dt.Rows[0]["screenlockkey"].ToString();
            #endregion

            #region 模式设置

            #region 练习模式            
            if (dt.Rows[0]["is_exam"].ToString() == "1")
            {
                //不显示成绩，不上传，不锁屏
                radioButton2.Checked = true;
                gbxSetting.Text = "练习模式";
                checkBox1.Checked = false;
                checkBox2.Checked = false;
                checkBox3.Checked = false;
            }
            #endregion

            #region 考试模式
            if (dt.Rows[0]["is_exam"].ToString() == "0")
            {
                //不显示成绩，上传，锁屏
                radioButton1.Checked = true;
                gbxSetting.Text = "考试模式";
                checkBox1.Checked = false;
                checkBox2.Checked = true;
                checkBox3.Checked = true;
            }
            #endregion

            #region 自定义模式
            if (dt.Rows[0]["is_exam"].ToString() == "2")
            {
                gbxSetting.Text = "自定义模式";
                radioButton3.Checked = true;
                if (dt.Rows[0]["is_showScore"].ToString() == "1")//显示成绩
                {
                    checkBox1.Checked = true;
                }
                else
                {
                    checkBox1.Checked = false;
                }
                if (dt.Rows[0]["is_upload"].ToString() == "0")//上传
                {
                    checkBox2.Checked = true;
                }
                else
                {
                    checkBox2.Checked = false;
                }
                if (dt.Rows[0]["is_lockScreen"].ToString() == "0")//锁屏
                {
                    checkBox3.Checked = true;
                }
                else
                {
                    checkBox3.Checked = false;
                }
            }

            #endregion 

            #endregion

        }
        #endregion

        #region 更新SetUP表数据-李金颖-2018年12月29日
        /// <summary>
        /// 更新SetUP表数据
        /// </summary>
        /// <param name="setup">实体</param>
        /// <param name="name">要更新的列名</param>
        public void UpdateSetUp(SetUp setup, string name)
        {
            DataTable dt = new LogBll().QuerySetUpBll();
            switch (name)
            {
                case "is_showScore":
                    dt.Rows[0]["is_showScore"] = setup.is_showScore;
                    break;
                case "is_upload":
                    dt.Rows[0]["is_upload"] = setup.is_upload;
                    break;
                case "is_lockScreen":
                    dt.Rows[0]["is_lockScreen"] = setup.is_lockScreen;
                    break;
                case "is_exam":
                    dt.Rows[0]["is_exam"] = setup.is_exam;
                    break;
                case "examTime":
                    dt.Rows[0]["examTime"] = setup.examTime;
                    break;
                case "noticeTime":
                    dt.Rows[0]["noticeTime"] = setup.noticeTime;
                    break;
                case "screenlockkey":
                    dt.Rows[0]["screenlockkey"] = setup.screenlockkey;
                    break;
            }
            setup.is_showScore = (int)dt.Rows[0]["is_showScore"];
            setup.is_upload = (int)dt.Rows[0]["is_upload"];
            setup.is_lockScreen = (int)dt.Rows[0]["is_lockScreen"];
            setup.is_exam = (int)dt.Rows[0]["is_exam"];
            setup.examTime = (int)dt.Rows[0]["examTime"];
            setup.noticeTime = (int)dt.Rows[0]["noticeTime"];
            setup.screenlockkey = (int)dt.Rows[0]["screenlockkey"];


            //更新到数据库
            ExamConfigBll exambll = new ExamConfigBll();
            exambll.UpdateSetUp(setup);
        } 
        #endregion

        #endregion
    }
}
